﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.DanhSachHocBongService;
namespace eTMS.Controllers{
public class DanhSachHocBongControllerBase : CommonController{
	public DanhSachHocBongControllerBase()
		: base()
	{	
		this.dao = new DanhSachHocBongServiceClient();
	}
	~DanhSachHocBongControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private DanhSachHocBongEntity oDanhSachHocBong;	
	public DanhSachHocBongEntity DanhSachHocBongObject{
		get{
			return oDanhSachHocBong;
		}
		set{
			oDanhSachHocBong=value;
		}
	}
	protected DanhSachHocBongServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSDanhSachHocBong(false);			
			return _Table;
		}
	}
	public void LoadAllDSDanhSachHocBong(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oDanhSachHocBong==null)return false;
		bool result=true;
		try{
			oDanhSachHocBong = dao.SaveRet(oDanhSachHocBong);
		}
		catch{
			result=false;
		}
		if(result){
			_ListDanhSachHocBong.Add(oDanhSachHocBong);
		}
		return result;
	}
	public bool Update(){
		if(oDanhSachHocBong==null)return false;
		return dao.Update(oDanhSachHocBong);
	}
	public bool Delete(){
		if(oDanhSachHocBong==null)return false;
		bool retvalue = dao.Delete(oDanhSachHocBong);
		if(retvalue){			
			_ListDanhSachHocBong.Remove(oDanhSachHocBong);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 DanhSachHocBong_ID){
		bool retvalue = dao.DeleteByID(DanhSachHocBong_ID);		
		if(retvalue){
			DanhSachHocBongEntity oDeleted=FindDanhSachHocBong(DanhSachHocBong_ID);
			if(oDeleted!=null){
				_ListDanhSachHocBong.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oDanhSachHocBong = new DanhSachHocBongEntity();
	}
    //Copy data from data row in grid to object
    public DanhSachHocBongEntity MapDataRowToObject(DataRow row){
		DanhSachHocBongEntity o = new DanhSachHocBongEntity();
		 if(!row.IsNull("HocKy_ID"))
			o.HocKy_ID=(Int32)(row["HocKy_ID"]);
		 if(!row.IsNull("MucHocBong_ID"))
			o.MucHocBong_ID=(Int32)(row["MucHocBong_ID"]);
		 if(!row.IsNull("NamHoc_ID"))
			o.NamHoc_ID=(Int32)(row["NamHoc_ID"]);
		 if(!row.IsNull("SinhVien_ID"))
			o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 if(!row.IsNull("DanhSachHocBong_ID"))
		    o.DanhSachHocBong_ID=(Int32)(row["DanhSachHocBong_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(DanhSachHocBongEntity o,DataRow row){
		row["HocKy_ID"]=o.HocKy_ID;
		row["MucHocBong_ID"]=o.MucHocBong_ID;
		row["NamHoc_ID"]=o.NamHoc_ID;
		row["SinhVien_ID"]=o.SinhVien_ID;
		row["DanhSachHocBong_ID"]=o.DanhSachHocBong_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.DanhSachHocBongObject,row);
    }
	public void LoadObject(Int32 DanhSachHocBong_ID){
		DanhSachHocBongEntity oLoad=FindDanhSachHocBong(DanhSachHocBong_ID);
		if(oLoad==null){
			oDanhSachHocBong=this.LoadDanhSachHocBong(DanhSachHocBong_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oDanhSachHocBong.IsNullDanhSachHocBong_ID;
			if(!bNotIsNull){
				this.ListDanhSachHocBong.Add(oDanhSachHocBong);
			}
		}
		else{
			oDanhSachHocBong=oLoad;
		}
	}
	#region CachedList DanhSachHocBong
	private static List<DanhSachHocBongEntity> _ListDanhSachHocBong=new List<DanhSachHocBongEntity>();
    public List<DanhSachHocBongEntity>ListDanhSachHocBong{
		get{
			return _ListDanhSachHocBong;
		}
	}
	public static List<DanhSachHocBongEntity>ListDanhSachHocBongStatic{
		get{
			return _ListDanhSachHocBong;
		}
	}
	public DanhSachHocBongEntity FindDanhSachHocBong(Int32 DanhSachHocBong_ID){
		DanhSachHocBongEntity oResult=this.ListDanhSachHocBong.Find(delegate(DanhSachHocBongEntity oFind){
			bool result=true; 
			result&=oFind.DanhSachHocBong_ID.Equals(DanhSachHocBong_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllDanhSachHocBong(bool clearCache){
		if(_ListDanhSachHocBong.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		DanhSachHocBongServiceClient dao = new DanhSachHocBongServiceClient();
		_ListDanhSachHocBong=new List<DanhSachHocBongEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public DanhSachHocBongEntity LoadDanhSachHocBong(Int32 DanhSachHocBong_ID){			
		DanhSachHocBongEntity o=dao.SelectOne(DanhSachHocBong_ID);		
		return o;
	}
	#endregion
}
}
